﻿
using System;
using System.Management;

class WMIHelper
{

  public WMIHelper( )
  {
    mObject = null;
  }

  public WMIHelper( ManagementObject obj )
  {
    mObject = obj;
  }

  // DIAGNOSTIC OUTPUT //

  public static void WriteNetInformation( ManagementObject adapter )
  {
    string name = adapter["Name"].ToString();
    Console.WriteLine(name);
    Console.WriteLine(string.Empty.PadLeft(name.Length, '='));
    Console.WriteLine("  Adapter Type ............................ : {0}", adapter["AdapterType"]);
    Console.WriteLine("  Enabled ................................. : {0}", adapter["NetEnabled"]);
    Console.WriteLine("  Physical Address ........................ : {0}", adapter["MACAddress"]);
    Console.WriteLine("  Connection Status ....................... : {0}",
      new NetConnectionStatus((UInt16)adapter["NetConnectionStatus"]));
  }

  static void WriteNetInformation( Predicate<ManagementObject> pred )
  {
    Console.WriteLine("Using Windows Management Instrumentation (WMI)");
    Console.WriteLine("==============================================");
    Console.WriteLine();

    using ( ManagementClass adapterManager = new ManagementClass("Win32_NetworkAdapter") )
    {
      ManagementObjectCollection adapterCollection = adapterManager.GetInstances();

      foreach ( ManagementObject adapter in adapterCollection )
      {
        if ( !pred(adapter) )
          continue;

        WriteNetInformation(adapter);

        Console.WriteLine();
      }
    }
  }

  // CONTROLS //

  public static UInt32 Enable( ManagementObject m )
  {
    return (UInt32)m.InvokeMethod("Enable", new object[0]);
  }

  public static UInt32 Disable( ManagementObject m )
  {
    return (UInt32)m.InvokeMethod("Disable", new object[0]);
  }

  // PREDICATES //

  public bool IsPhysical( ManagementObject obj )
  {
    return (bool)obj["PhysicalAdapter"] == true;
  }

  // PROPERTIES //

  string Name { get { return (string)mObject["Name"]; } }

  // DATA //

  ManagementObject mObject;

}
